package com.project.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.*;

import com.project.entity.ProjectManagement;

public class POITest {
	private String path;
	private String fileName;
	private int fileType;

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getFileType() {
		return fileType;
	}

	public void setFileType(int fileType) {
		this.fileType = fileType;
	}

	@SuppressWarnings("deprecation")
	public List<ProjectManagement> fileImport() {
		List<ProjectManagement> tmplist=new ArrayList();
		try {
			InputStream input = new FileInputStream(path+"\\"+fileName);
			POIFSFileSystem fs = new POIFSFileSystem(input);
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			HSSFSheet sheet = wb.getSheetAt(0);
			ProjectManagement tmp;
			double year = 0.0;//用来存储年份。
			// Iterate over each row in the sheet
			Iterator rows = sheet.rowIterator();
			while (rows.hasNext()) {
				HSSFRow row = (HSSFRow) rows.next();
				tmp = new ProjectManagement();
				System.out.println("Row #" + row.getRowNum());
				// Iterate over each cell in the row and print out the cell"s
				// content
				Iterator cells = row.cellIterator();
				while (cells.hasNext()) {
					HSSFCell cell = (HSSFCell) cells.next();
					System.out.println("Cell #" + cell.getCellNum());
					switch (cell.getCellNum()) {
					case 1:
						System.out.println(cell.getStringCellValue());
						tmp.setProjectnumber(cell.getStringCellValue());
						break;
					case 2:
						System.out.println(cell.getStringCellValue());
						tmp.setProjectname(cell.getStringCellValue());
						break;
					case 4:
						System.out.println(cell.getStringCellValue());
						tmp.setUnitedunitone(cell.getStringCellValue());
						break;
					case 5:
						System.out.println(cell.getStringCellValue());
						tmp.setUnitedunittwo(cell.getStringCellValue());
						break;
					case 7:
						System.out.println(cell.getStringCellValue());
						tmp.setEstablishprojectyear(cell.getStringCellValue());
						break;
					case 8:
						System.out.println(cell.getStringCellValue());
						tmp.setMainmanagingdepartment(cell.getStringCellValue());
						break;
					case 10:
						System.out.println(cell.getStringCellValue());
						tmp.setProjectplancatagory(cell.getStringCellValue());
						break;
					case 11:
						System.out.println(cell.getStringCellValue());
						tmp.setField(cell.getStringCellValue());
						break;
					case 12:
						System.out.println(cell.getStringCellValue());
						tmp.setSubfield(cell.getStringCellValue());
						break;
					case 13:
						System.out.println(cell.getStringCellValue());
						tmp.setIssuefilenumber(cell.getStringCellValue());
						break;
					case 14:
						System.out.println(cell.getNumericCellValue());
						year = cell.getNumericCellValue()-1900.0;	
						break;
					case 15:
						System.out.println(cell.getNumericCellValue());
						if(year==0.0){
							break;
						}
						Date contractBegin = new Date();
						contractBegin.setYear((int)year);
						contractBegin.setMonth((int)(cell.getNumericCellValue())-1);
						tmp.setContractbegin(contractBegin);
						year=0.0;
						break;//将第十四和第十五列数据组成一个Date类型的实例赋值给临时引用tmp
					case 16:
						System.out.println(cell.getNumericCellValue());
						year = cell.getNumericCellValue()-1900.0;	
						break;
					case 17:
						System.out.println(cell.getNumericCellValue());
						if(year==0.0){
							break;
						}
						Date contractEnd = new Date();
						contractEnd.setYear((int)year);
						contractEnd.setMonth((int)(cell.getNumericCellValue())-1);
						tmp.setContractend(contractEnd);
						year=0.0;
						break;
					case 18:
						System.out.println(cell.getNumericCellValue());
						tmp.setWinbidmoney((float)cell.getNumericCellValue());
						break;
					case 20:
						System.out.println(cell.getStringCellValue());
						tmp.setOfficephone(cell.getStringCellValue());
						break;
					case 21:
						System.out.println(cell.getStringCellValue());
						tmp.setMobilephone(cell.getStringCellValue());
						break;
					case 23:
						System.out.println(cell.getStringCellValue());
						tmp.setPhoneone(cell.getStringCellValue());
						break;
					case 24:
						System.out.println(cell.getStringCellValue());
						tmp.setPhonetwo(cell.getStringCellValue());
						break;
					case 26:
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_NUMERIC:
							System.out.println(cell.getNumericCellValue()+"111111111111");
							year = cell.getNumericCellValue();	
							break;
						case HSSFCell.CELL_TYPE_STRING:
							System.out.println(cell.getStringCellValue()+"222222222222222");
							year = Double.valueOf(cell.getStringCellValue());	
							break;
						case HSSFCell.CELL_TYPE_BOOLEAN:
							System.out.println(cell.getBooleanCellValue()+"33333333333");
							break;
						case HSSFCell.CELL_TYPE_FORMULA:
							System.out.println(cell.getCellFormula()+"44444444444");
							break;
						default:
							System.out.println("unsuported sell type");
							break;
						}
						
						break;
					case 27:
						System.out.println(cell.getNumericCellValue());
						if(year==0.0){
							break;
						}
						
						int month=2;
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_NUMERIC:
							System.out.println(cell.getNumericCellValue()+"111111111111");
							month = (int)cell.getNumericCellValue();	
							break;
						case HSSFCell.CELL_TYPE_STRING:
							System.out.println(cell.getStringCellValue()+"222222222222222");
							month = Integer.valueOf(cell.getStringCellValue());	
							break;
						case HSSFCell.CELL_TYPE_BOOLEAN:
							System.out.println(cell.getBooleanCellValue()+"33333333333");
							break;
						case HSSFCell.CELL_TYPE_FORMULA:
							System.out.println(cell.getCellFormula()+"44444444444");
							break;
						default:
							System.out.println("unsuported sell type");
							break;
						}
						
						
						Date checkdate = new Date();
						checkdate.setYear((int)year-1900);
						checkdate.setMonth(month-1);
						tmp.setCheckdate(checkdate);
						year=0.0;
						break;
					case 28:
						System.out.println(cell.getStringCellValue());
//						tmp.setProjectschedule(cell.getStringCellValue());
						break;
					case 29:
						System.out.println(cell.getStringCellValue());
						tmp.setProjectschedule(cell.getStringCellValue());
						break;
					case 30:
						System.out.println(cell.getStringCellValue());
						tmp.setProjectchange(cell.getStringCellValue());
						break;
					case 31:
						System.out.println(cell.getStringCellValue());
						tmp.setInventpatentapplication((int)(cell.getNumericCellValue()));
						break;
					case 32:
						System.out.println(cell.getStringCellValue());
						tmp.setInventpatentauthorization((int)(cell.getNumericCellValue()));
						break;
					case 33:
						System.out.println(cell.getStringCellValue());
						tmp.setUsenewapplication((int)(cell.getNumericCellValue()));
						break;
					case 34:
						System.out.println(cell.getStringCellValue());
						tmp.setUsenewauthorization((int)(cell.getNumericCellValue()));
						break;
					case 35:
						System.out.println(cell.getStringCellValue());
						tmp.setAppearancedesignapplication((int)(cell.getNumericCellValue()));
						break;
					case 36:
						System.out.println(cell.getStringCellValue());
						tmp.setAppearancedesignauthorization((int)(cell.getNumericCellValue()));
						break;
					case 37:
						System.out.println(cell.getStringCellValue());
						tmp.setForeignpatent((int)(cell.getNumericCellValue()));
						break;
					case 38:
						System.out.println(cell.getStringCellValue());
						tmp.setNewtechnology((int)(cell.getNumericCellValue()));
						break;
					case 39:
						System.out.println(cell.getStringCellValue());
						tmp.setNewproduct((int)(cell.getNumericCellValue()));
						break;
					case 40:
						System.out.println(cell.getStringCellValue());
						tmp.setNewmaterial((int)(cell.getNumericCellValue()));
						break;
					case 41:
						System.out.println(cell.getStringCellValue());
						tmp.setNewequipment((int)(cell.getNumericCellValue()));
						break;
					case 42:
						System.out.println(cell.getStringCellValue());
						tmp.setComputersoftware((int)(cell.getNumericCellValue()));
						break;
					case 43:
						System.out.println(cell.getStringCellValue());
						tmp.setPaper((int)(cell.getNumericCellValue()));
						break;
					case 44:
						System.out.println(cell.getStringCellValue());
						tmp.setTechnologystandard((int)(cell.getNumericCellValue()));
						break;
					case 45:
						System.out.println(cell.getStringCellValue());
						tmp.setNationalaward((int)(cell.getNumericCellValue()));
						break;
					case 46:
						System.out.println(cell.getStringCellValue());
						tmp.setProvincialaward((int)(cell.getNumericCellValue()));
						break;
					case 47:
						System.out.println(cell.getStringCellValue());
						tmp.setCityaward((int)(cell.getNumericCellValue()));
						break;
					case 48:
						System.out.println(cell.getStringCellValue());
						tmp.setDoctor((int)(cell.getNumericCellValue()));
						break;
					case 49:
						System.out.println(cell.getStringCellValue());
						tmp.setMaster((int)(cell.getNumericCellValue()));
						break;
					case 50:
						System.out.println(cell.getStringCellValue());
						tmp.setUndergraduate((int)(cell.getNumericCellValue()));
						break;
					case 51:
						System.out.println(cell.getStringCellValue());
						tmp.setFundsavailable((float)(cell.getNumericCellValue()));
						break;
					case 52:
						System.out.println(cell.getStringCellValue());
						tmp.setFundsinvestment((float)(cell.getNumericCellValue()));
						break;
					case 53:
						System.out.println(cell.getStringCellValue());
						tmp.setBenefitacquired((float)(cell.getNumericCellValue()));
						break;
					case 54:
						System.out.println(cell.getStringCellValue());
						tmp.setProjectsummary(cell.getStringCellValue());
						break;						
					default:
						System.out.println("unsuported sell type");
						break;
					}
					
				}
				tmplist.add(tmp);
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return tmplist;
	}

	public boolean fileExport() {
		return false;
	}

}
